<?php
/**
 * Wallet.php
 *
 * @author 周剑锋 <camfee@foxmail.com>
 * @date   17-8-31 上午10:39
 *
 */

namespace controls\MobileApi\Center;

use Admin\SiteStatus;
use Center\CoinLog;
use Center\Goods;
use MobileApi\ApiBase;
use Center\User as CUser;

/**
 * 用户钱包相关
 *
 * @package Center
 * @author  周剑锋 <camfee@foxmail.com>
 * @since   v1.0.0   2017-08-31
 */
class Wallet extends ApiBase
{
    /**
     * 获取钱包首页信息
     *
     * <pre>
     * GET:
     *      无传入参数
     * </pre>
     *
     * @return void|string 返回JSON数据
     *
     * <pre>
     * {
     *     "Status": 200,
     *     "Result": {
     *         "JCoinNum": 666, // 金果数
     *         "GoodsList": [
     *             {
     *                 "GoodsId": 1, // 商品id
     *                 "GoodsAppleId":'', // 苹果商品id
     *                 "GoodsName": "6金果", // 商品名称
     *                 "Price": "6.00"  // 价格
     *                 "JCoin": 6  // 金果数量
     *                 "Donate": 0  // 赠送金果数量
     *             }
     *         ],
     *         "CoinDesc":"一个金果等于一元人民币"
     *     },
     *     "ServerTime": 1511444425
     * }
     * 异常状态
     * 201: 参数错误
     *
     * </pre>
     */
    public function getIndex()
    {
        $uid = $this->isLogin(true);
        $user_info = CUser::getInfoByIds($uid);
        if (empty($user_info)) {
            $this->output(201, '参数错误');
        }
        $goods = Goods::getAllGoods();
        $desc = loadconf('wallet/desc');
        $list = [];
        if (!empty($goods['data'])) {
            foreach ($goods['data'] as $v) {
                $list[] = [
                    'GoodsId' => $v['Id'],
                    'GoodsAppleId' => $v['AppleId'],
                    'GoodsName' => $v['Title'] . (!empty($v['Donate']) ? "赠送{$v['Donate']}个金果" : ''),
                    'Price' => $v['Price'],
                    'JCoin' => $v['JCoin'],
                    'Donate' => $v['Donate']
                ];
            }
        }
        $data = [
            'JCoinNum' => (int)$user_info['JCoin'],
            'GoodsList' => $list,
            'CoinDesc' => $desc['desc'],
        ];

        $this->output(200, $data);
    }

    /**
     * 交易记录
     *
     * <pre>
     * GET:
     *     offset: 可选， 偏移量 默认0
     *     limit:  可选， 每次数量, 最高不超过50 默认20
     * </pre>
     *
     * @return void|string 返回JSON数据
     *
     * <pre>
     * {
     *     "Status": 200,
     *     "Result": {
     *         "JCoinNum": 666, // 我的金果数
     *         "Total": 21, // 总数
     *         "List": [
     *             {
     *                 "Type": 100,  // 类型 >= 100 增加  < 100 消费
     *                 "Title": "充值", // 说明
     *                 "Num": "+100", // 数量
     *                 "Time": "2016-09-08 09:12" // 时间
     *             },...
     *         ]
     *     },
     *     "ServerTime": 1511445158
     * }
     * 异常状态
     * 201: 参数错误
     * </pre>
     */
    public function getCoinList()
    {
        $uid = $this->isLogin(true);

        $offset = (int)$_GET['offset'];
        $limit = (int)$_GET['limit'];
        $offset = max(0, $offset);
        $limit = $limit < 1 ? 20 : min($limit, 50);

        $user_info = CUser::getInfoByIds($uid);
        if (empty($user_info)) {
            $this->output(201, '参数错误');
        }
        $list_ids = CoinLog::getUserList($uid, $offset, $limit);
        $list = [];
        if (!empty($list_ids['data'])) {
            $list_info = CoinLog::getInfoByIds($list_ids['data']);
            $type_text = loadconf('wallet/coin')['type'];
            foreach ($list_info as $v) {
                $list[] = [
                    'Type' => $v['Type'],
                    'Title' => isset($type_text[$v['Type']]) ? $type_text[$v['Type']] : (string)$v['Info'],
                    'Num' => $v['Type'] >= 100 ? '+' . $v['CoinNum'] : '-' . $v['CoinNum'],
                    'Time' => $v['CreateTime']
                ];
            }
        }
        $data = [
            'JCoinNum' => (int)$user_info['JCoin'],
            'Total' => (int)$list_ids['count'],
            'List' => $list
        ];

        $this->output(200, $data);
    }

    /**
     * 预支金果
     *
     * <pre>
     * GET:
     *     offset: 可选， 偏移量 默认0
     *     limit:  可选， 每次数量, 最高不超过50 默认20
     * </pre>
     *
     * @return void|string 返回JSON数据
     *
     * <pre>
     * {
     *     "Status": 200,
     *     "Result": {
     *         "Total": 21, // 总数
     *         "List": [
     *             {
     *                 "Num": "100个", // 数量
     *                 "Time": "2016-09-08 09:12" // 时间
     *             },...
     *         ]
     *     },
     *     "ServerTime": 1511445158
     * }
     * 异常状态
     * 201: 参数错误
     * </pre>
     */
    public function getAdvanceCoinList()
    {
        $uid = $this->isLogin(false);

        $offset = (int)$_GET['offset'];
        $limit = (int)$_GET['limit'];
        $offset = max(0, $offset);
        $limit = $limit < 1 ? 20 : min($limit, 50);

        $list_info = SiteStatus::getStatisticList(SiteStatus::TYPE_COIN_DONATE, $offset, $limit);
        $list = [];
        if (!empty($list_info['data'])) {
            foreach ($list_info['data'] as $v) {
                $list[] = [
                    'Num' => $v['Num'] . '个',
                    'Time' => $v['CreateDate']
                ];
            }
        }
        $data = [
            'Total' => intval($list_info['count']),
            'List' => $list
        ];

        $this->output(200, $data);
    }
}
